import { Controller } from "../../mvc/core/Controller";
import { PreLoginProxy } from "../../common/preLoadPage/PreLoginProxy";
import { GameRMI } from "../net/rmi/GameRMI";
import { AMI_ILoginGame_loginGame } from "../../../Message/Game/AMI_ILoginGame_loginGame";
import { ParamsConst } from "../../common/global/ParamsConst";
import { Cache } from "../cache/Cache";
import { SLoginGameReturn } from "../../../Message/Game/SLoginGameReturn";
import { PHPSender } from "../../../extend/php/PHPSender";
import { SenderType } from "../../../extend/php/SenderType";
// import { ClockManager } from "../manager/ClockManager";
// import { GameManager } from "../manager/GameManager";
/* import { CacheManager } from "../../../com/gengine/manager/CacheManager";
import { Global } from "../../../com/gengine/global/Global"; */
// import { LocalCacheSetWindow } from "../view/walkthrough/LocalCacheSetWindow";
// import { ResManager } from "../../common/ResManager";
import { Dispatcher } from "../../mvc/core/Dispatcher";
// import { DataEvent } from "../events/DataEvent";
// import { EventName } from "../mvc/EventName";
import { Log } from "../../../com/gengine/debug/Log";
// import { TimerTest } from "../../../com/gengine/utils/TimerTest";
// import { GameLoadBar } from "../../common/preLoadPage/GameLoadBar";
// import { ResConfig } from "../resource/ResConfig";
// import { ConstConfig } from "../resource/ConstConfig";
/* import { FilterText } from "../../../com/gengine/utils/FilterText";
import { ConfigManager } from "../../../com/gengine/resource/ConfigManager";
import { ChatFraudFilter } from "../../../com/gengine/utils/ChatFraudFilter"; */
// import { NPCConfig } from "../resource/NPCConfig";
// import { MusicsAndSoundsConfig } from "../resource/MusicsAndSoundsConfig";
// import { SkillModelConfig } from "../resource/SkillModelConfig";
// import { GameController } from "../mvc/GameController";
// import { GameLayout } from "../GameLayout";
// import { ModuleConfig } from "../resource/ModuleConfig";
/* import { MapConfig } from "../../../com/gengine/game/MapConfig";
import { PathConst } from "../../common/global/PathConst"; */
// import { Game } from "../Game";
// import { GameScene } from "../scene/GameScene";
// import { MapFileUtil } from "../scene/map/MapFileUtil";
// import { GameMapUtil } from "../scene/map/GameMapUtil";
import { Exception } from "../../../Framework/Util/Exception";
// import { MsgManager } from "../manager/MsgManager";
// import { RoleSelectPage } from "../../common/preLoadPage/RoleSelectPage";
import { SMyRole } from "../../../Message/Login/SMyRole";
import { DataEvent } from "../../../flash/events";
import { EventName } from "../mvc/EventName";
type int = number;
//class LoginController

export class LoginController extends Controller {
  constructor() {
    super();
    if (LoginController._instance) {
      throw new Error("LoginController 单例！");
    }
    return;
  }

  public loadGame(): void {
    var loc1 = PreLoginProxy.instance.sloginReturn;
    GameRMI.instance.rmiSession =
      PreLoginProxy.instance.gameSession.gmaeSession;
    GameRMI.instance.loginGameProxy.loginGame_async(
      new AMI_ILoginGame_loginGame(
        this.loginGameSuccess,
        this.loginGameException
      ),
      loc1.playerId,
      ParamsConst.instance.username,
      loc1.name,
      loc1.sessionKey.id,
      loc1.sessionKey.key,
      ParamsConst.Version,
      ParamsConst.instance.currentCity
    );
    Cache.instance.login.loginData = PreLoginProxy.instance.slogin;
    Cache.instance.login.loginReturn = loc1;
    return;
  }

  private loginGameSuccess(arg1: Object, arg2: SLoginGameReturn): void {
    var loc1 = null;
    PHPSender.sendGift(arg2.player.playerId);
    if (PHPSender.isCreateRole) {
      loc1 = ParamsConst.instance.gameMsgURL;
      if (!(loc1 == null) && !(loc1 == "")) {
        // flash.net.sendToURL(
        //   new Object(
        //     /* flash.net.URLRequest */ loc1 +
        //       "&username=" +
        //       encodeURI(ParamsConst.instance.username) +
        //       "&rolename=" +
        //       encodeURI(arg2.player.name)
        //   )
        // );
      }
      PHPSender.sendToURLByPHP(SenderType.LoginGameSuccess);
    }
    PHPSender.sendToURLByPHP(
      SenderType.FlashPlayerVersion,
      this.getFlashPlayerVersion()
    );
    PHPSender.sendToURLByPHP(
      SenderType.ScreenResolution,
      this.getScreenResolution()
    );
    PHPSender.isCreateRole = false;
    // ClockManager.instance.roleCreateDate = arg2.player.createDt;
    // ClockManager.instance.setNowTimer(arg2.sysDt);
    this._sloginGameReturn = arg2;
    Cache.instance.login.loginGame = arg2;
    this._mapID = this._sloginGameReturn.pos.map;
    this._currentGridX = this._sloginGameReturn.pos.x;
    this._currentGridY = this._sloginGameReturn.pos.y;
    this._proxyID = this._sloginGameReturn.pos.proxyId;
    this._serverID = this._sloginGameReturn.pos.serverId;
    this.initApp();
    // GameManager.instance.init();
    // if (ClockManager.instance.isNotCreateRoleDay()) {
    //   if (CacheManager.instance.isCache == false) {
    //     if (Global.isDebugModle == false) {
    //       LocalCacheSetWindow.instance.show();
    //     }
    //   }
    // }
    // ResManager.instance.loadLevelRes();
    Dispatcher.dispatchEvent(new DataEvent(EventName.LoginGameSuccess));
    if (ParamsConst.instance.timerTestType > 0) {
      setTimeout(this.startTimerTest, 1000 * 10);
      Log.debug("TimerTest start:" + ParamsConst.instance.timerTestType);
    }
    setTimeout(this.getShopMallPanicMsg, 1000 * 10);
    // cache.spiritEquip.initData();
    Dispatcher.dispatchEvent(new DataEvent(EventName.ChangeHeadIconGetInfo));
    return;
  }

  private startTimerTest(): void {
    // var loc1 = new TimerTest(Global.stage);
    // TimerTest.Delay = ParamsConst.instance.timerTestType;
    // loc1.start();
    return;
  }

  private getShopMallPanicMsg(): void {
    Dispatcher.dispatchEvent(new DataEvent(EventName.GetPanicBuyConfig));
    return;
  }

  private initApp(): void {
    this.initConfig();
    // if (GameLoadBar.instance) {
    //   GameLoadBar.instance.hide();
    // }
    this.initUI();
    this.initModules();
    this.initMap();
    return;
  }

  private initConfig(): void {
    // ResConfig.instance;
    // ConstConfig.instance;
    // FilterText.instance.setFilterStr(
    //   ConfigManager.instance.getStringByFileName("filterChat.txt")
    // );
    // var loc1 = ConfigManager.instance.getObjectByFileName(
    //   "ChatFraudFilter.xml"
    // );
    // if (loc1.filter) {
    //   if (typeof loc1.filter === "array") {
    //     ChatFraudFilter.source = loc1.filter as Array<any>;
    //   } else {
    //     ChatFraudFilter.source = [loc1.filter];
    //   }
    // }
    // NPCConfig.instance.init();
    // MusicsAndSoundsConfig.instance;
    // SkillModelConfig.instance;
    return;
  }

  private initUI(): void {
    // GameController.gameui.view.show();
    // GameLayout.instance.init();
    return;
  }

  private initModules(): void {
    // ModuleConfig.instance.init();
    return;
  }

  private initMap(): void {
    // MapConfig.mapPath = PathConst.mapPath;
    // MapConfig.modelPath = PathConst.modelPath;
    // Game.scene = new GameScene(Global.application);
    // GameController.scene.view;
    // MapFileUtil.mapID = this._mapID;
    // MapFileUtil.proxyID = this._proxyID;
    // MapFileUtil.serverID = this._serverID;
    // GameMapUtil.curMapState.initMapId(
    //   this._mapID,
    //   this._proxyID,
    //   this._serverID
    // );
    // Game.scene.setPlayerPoint(
    //   this._mapID,
    //   this._currentGridX,
    //   this._currentGridY
    // );
    // Dispatcher.dispatchEvent(new DataEvent(EventName.ChangeScene));
    // Log.debug("地图ID:" + this._mapID);
    // GameLayout.instance.resize(true);
    return;
  }

  public loginGameException(arg1: Exception): void {
    // MsgManager.alertError(arg1.code);
    if (PHPSender.isCreateRole) {
      PHPSender.sendToURLByPHP(SenderType.LoginGameFail, arg1.message);
    }
    return;
  }

  public showRoleSelectPage(): void {
    // if (!this.roleSelectPage) {
    //   this.roleSelectPage = new RoleSelectPage();
    //   this.roleSelectPage.addEventListener(
    //     RoleSelectPage.ResourceLoaded,
    //     this.onResourceLoaded
    //   );
    //   this.roleSelectPage.addEventListener(
    //     RoleSelectPage.EnterGame,
    //     this.onEnterGame
    //   );
    //   this.roleSelectPage.addEventListener(
    //     RoleSelectPage.SetMain,
    //     this.onSetMain
    //   );
    // }
    // Global.stage.addChild(this.roleSelectPage);
    return;
  }

  private onResourceLoaded(arg1: DataEvent): void {
    // if (GameLoadBar.instance) {
    //   GameLoadBar.instance.visible = false;
    // }
    return;
  }

  private onEnterGame(arg1: DataEvent<SMyRole>): void {
    PreLoginProxy.instance.doEnterGame(arg1.data!);
    return;
  }

  private onSetMain(arg1: DataEvent<SMyRole>): void {
    PreLoginProxy.instance.doSetMain(arg1.data!);
    return;
  }

  public hideRoleSelectPage(): void {
    // if (this.roleSelectPage && this.roleSelectPage.parent) {
    //   this.roleSelectPage.parent.removeChild(this.roleSelectPage);
    // }
    return;
  }

  private getFlashPlayerVersion(): string {
    // var loc3 = null;
    // var loc1 = flash.system.Capabilities.version;
    // Log.debug(loc1);
    // var loc2 = loc1.search(new RegExp("\\d"));
    // if (loc2 != -1) {
    //   loc1 = loc1.substr(loc2);
    //   loc3 = loc1.split(",");
    //   if (loc3.length == 4) {
    //     Log.debug("flash播放器版本信息:");
    //     Log.debug("主版本号: ", loc3[0]);
    //     Log.debug("次版本号: ", loc3[1]);
    //     Log.debug("生成版本号: ", loc3[2]);
    //     Log.debug("内部生成版本号: ", loc3[3]);
    //     return loc3[0] + "." + loc3[1];
    //   }
    //   Log.debug("flash 播放器的版本: ", loc1);
    //   return String(loc1);
    // }
    return "未知版本号";
  }

  private getScreenResolution(): string {
    // var loc3 = null;
    // var loc1 = flash.system.Capabilities.screenResolutionX;
    // var loc2 = flash.system.Capabilities.screenResolutionY;
    // if (loc1 > 0 && loc2 > 0) {
    //   loc3 = Math.round(loc1) + "*" + Math.round(loc2);
    //   Log.debug("显示器分辨率：", loc3);
    //   return loc3;
    // }
    // Log.debug("未知分辨率");
    return "未知分辨率";
  }

  public static get instance(): LoginController {
    if (!LoginController._instance) {
      LoginController._instance = new LoginController();
    }
    return LoginController._instance;
  }

  public /* var */ stage: any /* flash.display.DisplayObjectContainer */;

  private /* var */ _sloginGameReturn!: SLoginGameReturn;

  public /* var */ roleSelectPage!: any /* RoleSelectPage */;

  private /* var */ _mapID: int = 300103;

  private /* var */ _currentGridX: int = 200;

  private /* var */ _currentGridY: int = 200;

  private /* var */ _proxyID: int = 0;

  private /* var */ _serverID: int = 0;

  private static /* var */ _instance: LoginController;
}
